import { isResetOldPassword } from "@home/store";
import { fetchList } from ".";
import { operateType, modalInputValidators } from "./store";
import {
  password_old_verify,
  password_new_verify,
  password_new_confirm_verify,
} from "./utils";
import { ModalFormStateType } from "./type";
export const modalVisible = ref(false);
export const modalLoading = ref(false);
export const modalTitle = ref("");
export const modalInstance = ref<any>(null);
export const modalFormState = reactive<ModalFormStateType>({
  uid: undefined,
  password_old: undefined,
  password_new: undefined,
  password_new_confirmation: undefined,
});
export const modalConfig = () => {
  const modal_disable_ok_button = computed(() => {
    const { formState } = defineModal;
    return (
      isEmptyType(formState.password_new) ||
      isEmptyType(formState.password_new_confirmation) ||
      modalInputValidators.some((bool) => bool === true)
    );
  });
  const modalList = [
    {
      label: getProxyT("table71"),
      name: "password_new",
      maxlength: 13,
      type: "inputPassword",
      model: "password_new",
      placeholder: getProxyT("placeholder0"),
      rules: [
        {
          required: true,
          validator: password_new_verify({
            inputValidators: modalInputValidators,
            index0: 0,
            index1: 1,
          }),
          trigger: "change",
        },
      ],
    },
    {
      label: getProxyT("table70"),
      name: "password_new_confirmation",
      maxlength: 13,
      type: "inputPassword",
      model: "password_new_confirmation",
      placeholder: getProxyT("placeholder1"),
      rules: [
        {
          required: true,
          validator: password_new_confirm_verify({
            inputValidators: modalInputValidators,
            index0: 1,
            index1: 0,
          }),
          trigger: "change",
        },
      ],
    },
  ];
  const defineModal = {
    modalInstance,
    loading: modalLoading,
    visible: modalVisible,
    maskClosable: true,
    disable_ok_button: modal_disable_ok_button,
    disable_cancel_buttton: false,
    reverse: true,
    title: modalTitle,
    formState: modalFormState,
    event: {
      cancel: () => (isResetOldPassword.value = false),
    },
    on: {
      async finish(values: any) {
        try {
          modalLoading.value = true;
          if (isResetOldPassword.value) {
            const res: any = await userApi.updateUserPsd({
              password_old: modalFormState.password_old,
              password_new: modalFormState.password_new,
              password_new_confirmation:
                modalFormState.password_new_confirmation,
            });
            if (res.code != 200) return message.error(res.message);
            modalVisible.value = false;
            const userStore = useUserStore();
            Modal.success({
              title: $t("user12"),
              keyboard: false,
              okText: $t("user13"),
              style: {
                top: "35%",
              },
              onOk: () => userStore.logout(),
            });
            return;
          }
          switch (operateType.value) {
            case "update->password":
              const res: any = await userApi.updateAccountPsd({
                userId: modalFormState.uid,
                password_new: modalFormState.password_new,
                password_new_confirmation:
                  modalFormState.password_new_confirmation,
              });
              if (res.code != 200) return message.error(res.message);
              modalVisible.value = false;
              message.success($t("placeholder79"));
              break;
          }
          fetchList();
        } catch (error: any) {
          message.error(error.message);
        } finally {
          modalLoading.value = false;
        }
      },
    },
    modalList: computed(() =>
      isResetOldPassword.value
        ? [
            {
              label: getProxyT("table104"),
              name: "password_old",
              maxlength: 13,
              type: "inputPassword",
              model: "password_old",
              placeholder: getProxyT("placeholder57"),
              rules: [
                {
                  required: true,
                  validator: password_old_verify({
                    inputValidators: modalInputValidators,
                    index0: 2,
                    index1: 0,
                    index2: 1,
                  }),
                  trigger: "change",
                },
              ],
            },
            ...modalList,
          ]
        : modalList
    ),
  };
  return defineModal;
};
